using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using WhittleList.Bll;
using WhittleList.Bll.Exceptions;
using WhittleList.Helpers;
using System.Data.Linq;

namespace WhittleList.Controllers
{
    public class ItemController : BaseController
    {
        public ItemController()
            : this(new UserRepository(), new SiteRepository())
        {
        }

        public ItemController(ISiteRepository siteRepository)
            : this(new UserRepository(), siteRepository)
        {
        }

        public ItemController(IUserRepository userRepository)
            : this(userRepository, new SiteRepository())
        {
        }

        public ItemController(IUserRepository userRepository, ISiteRepository siteRepository)
            : base(userRepository, siteRepository)
        {
        }
 

        //
        // GET: /Item/

        public ActionResult Index(Site site, int sectionId)
        {
            var section = SiteRepository.Section_GetById(sectionId);
            if (site != null && section != null && section.Project.SiteId == site.Id)
            {
                var items = SiteRepository.Item_GetByAllBySectionId(sectionId).ToList();
                return View(items);
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        //
        // GET: /Item/Details/5

        public ActionResult Details(Site site, int id)
        {
            var item = SiteRepository.Item_GetById(id);
            if (site != null && item != null && item.Section.Project.SiteId == site.Id)
            {
                return View(item);
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        //
        // GET: /Item/Create

        public ActionResult Create(Site site, int sectionId)
        {
            var section = SiteRepository.Section_GetById(sectionId);
            if (site != null && section != null && section.Project.SiteId == site.Id)
            {
                return View();
            }
            else
            {
                throw new NotImplementedException();
            }
        } 

        //
        // POST: /Item/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Site site, int sectionId, string name, string notes, int importance)
        {
            var section = SiteRepository.Section_GetById(sectionId);
            if (site != null && section != null && section.Project.SiteId == site.Id)
            {
                var item = new Item();
                item.SectionId = sectionId;
                item.Importance = importance;
                item.Name = name;
                item.Notes = notes;

                SiteRepository.Insert(item);

                try
                {
                    SiteRepository.SaveChanges(CurrentUser);
                    return RedirectToAction("Details", "Project", new { id = section.Project.Id });
                }
                catch (DuplicateNameException)
                {
                    ViewData.ModelState.AddModelError("Name", string.Format("'{0}' already exists as an item in this section", name)); 
                }
                catch (ValidationException e)
                {
                    ViewData.ModelState.AddModelErrors(e.RuleViolations);
                }

                return View(item);
            }
            else
            {
                throw new NotImplementedException();
            }


        }

        //
        // GET: /Item/Edit/5
 
        public ActionResult Edit(Site site, int id)
        {
            var item = SiteRepository.Item_GetById(id);
            if (site != null && item != null && item.Section.Project.SiteId == site.Id)
            {
                return View(item);
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        //
        // POST: /Item/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Site site, int id, string name, string notes, int importance, Binary cid)
        {
            var item = SiteRepository.Item_GetById(id);
            if (site != null && item != null && item.Section.Project.SiteId == site.Id)
            {
                item.Importance = importance;
                item.Name = name;
                item.Notes = notes;

                try
                {
                    item.EnsureConcurrent(cid);
                    SiteRepository.SaveChanges(CurrentUser);
                    return RedirectToAction("Index");
                }
                catch (ConcurrencyException)
                {
                    ViewData.ModelState.AddModelError("_Form", "This record has been updated by another user. Please reload the page bofore saving");
                }
                catch (DuplicateNameException)
                {
                    ViewData.ModelState.AddModelError("Name", string.Format("'{0}' already exists as an item in this section", name));
                }
                catch (ValidationException e)
                {
                    ViewData.ModelState.AddModelErrors(e.RuleViolations);
                }

                return View(item);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
    }
}
